package com.gxgeek.topic;

import com.gxgeek.utils.ConnectionUtil;
import com.rabbitmq.client.*;

import java.io.IOException;

/**
 * @author gaoxing
 * @create 2018-05-17 下午2:51
 */
public class Recv1 {
    private final static String QUEUE_NAME = "test_queue_topic_1";//队列名称

    private final static String EXCHANGE_NAME="test_exchange_topic";//交换机名称

    public static void main(String[] argv) throws Exception {
        // 获取到连接以及mq通道
        Connection connection = ConnectionUtil.getConnection();
        Channel channel = connection.createChannel();

        // 声明队列
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);

        //绑定队列到交换机，并指定了一个通配符模式的key，来获取中国大类别下面的所有小类别消息。
        channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "china.#");
        // 同一时刻服务器只会发一条消息给消费者
        channel.basicQos(1);

        // 定义队列的消费者
        Consumer consumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                String message = new String(body, "UTF-8");
                System.out.println(" [consumer1] Received '" + message + "'");
            }
        };
        // 监听队列，手动返回完成
        channel.basicConsume(QUEUE_NAME, true, consumer);

    }

}
